/*
 *
 */

jsx3.Class.defineClass(
        "tibco.admin.enterpriseAssets.shdResDefs.ClientSSL.ShdSSLClientResDefs", //the full name of the class to create
        jsx3.lang.Object, //  extends class
        [tibco.admin.enterpriseAssets.shdResDefs.ShdResDefsInterface], // implements interface
        function (plugin) {

            plugin.prototype.details = null;
            // TODO: handle multiple instances?

            // Implementation of the ShdResDefsInterface
            plugin.instance = null

            plugin.prototype.initPlugin = function() {
                tibco.admin.Main.loadResource("tibco/admin/enterpriseAssets/shdResDefs/ClientSSL/jss/jss.xml", "tibco.admin", "jss_25G", "ljss", false);
            }

            plugin.prototype.destroyPlugin = function() {
            }

            plugin.getInstance = function(args) {
                if(!this.instance) {
                    this.instance = new tibco.admin.enterpriseAssets.shdResDefs.ClientSSL.ShdSSLClientResDefs();
                }
                return this.instance;
            }

            plugin.prototype.loadComponent = function(parent, master, id, name, description) {
                var me = this;
                this.details = new tibco.admin.enterpriseAssets.shdResDefs.ClientSSL.ShdSSLClientResDefDetails(master, parent, id,
                        name, description, me);
                this.details.loadView();
                return this.details;
            }

            plugin.prototype.getType = function() {
                return "SSL_CLIENT";
            }

            plugin.prototype.getAddText = function() {
                return tibco.admin.Main.getServer().getDynamicProperty("@AddText_SSLClient");
            }

            plugin.prototype.getImagePath = function() {
                return tibco.admin.Main.getServer().getDynamicProperty("@addin@ClientSSL Icon Path");
            }

            plugin.prototype.addSharedResourceDefinition = function(parent, master) {
                var me = this;
                this.details = new tibco.admin.enterpriseAssets.shdResDefs.ClientSSL.ShdSSLClientResDefDetails(master, parent, null,
                        null, null, me);
                this.details.loadAdd();
                return this.details;
            }
            // End implementation of the ShdResDefsInterface

        });

jsx3.Class.defineClass(
        "tibco.admin.enterpriseAssets.shdResDefs.ClientSSL.ShdSSLClientResDefDetails", //the full name of the class
        tibco.admin.enterpriseAssets.shdResDefs.ShdResDefDetails, //  extends ShdResDefDetails
        null, // implements no interface
        function (details) {
            details.ONVERIFYHOSTNAME = "tibco.admin.enterpriseAssets.shdResDefs.ClientSSL.ONVERIFYHOSTNAME";
            details.ONNOTVERIFYHOSTNAME = "tibco.admin.enterpriseAssets.shdResDefs.ClientSSL.ONNOTVERIFYHOSTNAME";
            details.prototype.subType = null;
            details.prototype.helpID = "EnterpriseAssets.SharedResourceDefinitions.SharedResourceDefinitions.SSLClient";

            details.prototype.init = function(master, parent, id, name, description, handler) {
                this.jsxsuper(master, parent, id, name, description, handler);
                this.editableComponentList.addAll(["selIdentity_25G", "selCipherStrength_25G", "radVerifyHostnameYes_25G",
                        "radVerifyHostnameNo_25G", "txtHostname_25G", "txtStoreURL_25G", "txtPassword_25G"], this.editableComponentList.size());
                this.rwComponentList.addAll(["selIdentity_25G", "selCipherStrength_25G", "blkVerifyHostnameYesNo_25G",  "txtHostname_25G", "txtStoreURL_25G", "txtPassword_25G"], this.rwComponentList.size());
                this.roComponentList.addAll(["lblcIdentity_25G", "lblcVerifyHostname_25G", "lblcHostname_25G", "lblcCipherStrength_25G", "lblcStoreURL_25G", "lblcPassword_25G"], this.roComponentList.size());
            }

            details.prototype.getType = function() {
                return tibco.admin.Main.getServer().getDynamicProperty("@SRDType_SSLClient");
            }

            details.prototype.getHelpId = function() {
                return this.helpID;
            }

            details.prototype.getDetailsOperation = function() {
                return "getSSLClientResourceDetails";
            }
            details.prototype.getDetailsOperationRules = function() {
                return "tibco/admin/enterpriseAssets/shdResDefs/ClientSSL/rules/getSSLClientResourceDetails.xml";
            }
            details.prototype.editOperation = function() {
                return "editSSLClientResource";
            }
            details.prototype.editOperationRules = function() {
                return "tibco/admin/enterpriseAssets/shdResDefs/ClientSSL/rules/editSSLClientResource.xml";
            }
            details.prototype.addOperation = function() {
                return "addSSLClientResource";
            }
            details.prototype.addOperationRules = function() {
                return "tibco/admin/enterpriseAssets/shdResDefs/ClientSSL/rules/addSSLClientResource.xml";
            }
            details.prototype.getDetailsComponent = function() {
                return "tibco/admin/enterpriseAssets/shdResDefs/ClientSSL/components/shdSSLClientResDefDetails.xml";
            }
            details.prototype.getSecondaryDetailsComponent = function() {
                return null;
            }

            // TODO: make name unique
            details.prototype.getDefaultName = function() {
                return tibco.admin.Main.getServer().getDynamicProperty("@defaultName_SSLClient");
            }
            details.prototype.getDefaultDescription = function() {
                return tibco.admin.Main.getServer().getDynamicProperty("@defaultDescription_SSLClient");
            }

            details.prototype.initializeGenericDetails = function(objEvent) {
                this.jsxsuper(objEvent);
            }

            details.prototype.initializeSpecificDetails = function(objEvent) {
                if(objEvent.status == "SUCCESS") {
                    var str = '<data jsxid="jsxroot">';
                    str += '<record jsxid="NO_RESTRICTION" jsxtext="' + tibco.admin.Main.getServer().getDynamicProperty("@@NO_RESTRICTION_25G") + '" />';
                    str += '<record jsxid="NO_EXPORT_GRADE" jsxtext="' + tibco.admin.Main.getServer().getDynamicProperty("@@NO_EXPORT_GRADE_25G") + '" />';
                    str += '<record jsxid="AT_LEAST_128_BIT" jsxtext="' + tibco.admin.Main.getServer().getDynamicProperty("@@AT_LEAST_128_BIT_25G") + '" />';
                    str += '<record jsxid="MORE_THAN_128_BIT" jsxtext="' + tibco.admin.Main.getServer().getDynamicProperty("@@MORE_THAN_128_BIT_25G") + '" />';
                    str += '<record jsxid="AT_LEAST_256_BIT" jsxtext="' + tibco.admin.Main.getServer().getDynamicProperty("@@AT_LEAST_256_BIT_25G") + '" />';
                    str += '</data>';
                    var sel = this.mainComponent.getDescendantOfName("selCipherStrength_25G");
                    tibco.admin.Main.getServer().getCache().clearById(sel.getXMLId());
                    sel.setXMLString(str);
                    sel.repaint();
                    this.mainComponent.getDescendantOfName("radVerifyHostnameYes_25G").subscribe(details.ONVERIFYHOSTNAME, this, this.onselectedRadVerifyHostnameYes_25G);
                    this.mainComponent.getDescendantOfName("radVerifyHostnameNo_25G").subscribe(details.ONNOTVERIFYHOSTNAME, this, this.onselectedRadVerifyHostnameNo_25G);
                }
                this.jsxsuper(objEvent);
            }

            details.prototype.initializeGenericAdd = function(objEvent) {
                this.jsxsuper(objEvent);
            }

            details.prototype.initializeSpecificAdd = function(objEvent) {
                if(objEvent.status == "SUCCESS") {
                    var str = '<data jsxid="jsxroot">';
                    str += '<record jsxid="NO_RESTRICTION" jsxtext="' + tibco.admin.Main.getServer().getDynamicProperty("@@NO_RESTRICTION_25G") + '" />';
                    str += '<record jsxid="NO_EXPORT_GRADE" jsxtext="' + tibco.admin.Main.getServer().getDynamicProperty("@@NO_EXPORT_GRADE_25G") + '" />';
                    str += '<record jsxid="AT_LEAST_128_BIT" jsxtext="' + tibco.admin.Main.getServer().getDynamicProperty("@@AT_LEAST_128_BIT_25G") + '" />';
                    str += '<record jsxid="MORE_THAN_128_BIT" jsxtext="' + tibco.admin.Main.getServer().getDynamicProperty("@@MORE_THAN_128_BIT_25G") + '" />';
                    str += '<record jsxid="AT_LEAST_256_BIT" jsxtext="' + tibco.admin.Main.getServer().getDynamicProperty("@@AT_LEAST_256_BIT_25G") + '" />';
                    str += '</data>';
                    var sel = this.mainComponent.getDescendantOfName("selCipherStrength_25G");
                    tibco.admin.Main.getServer().getCache().clearById(sel.getXMLId());
                    sel.setXMLString(str);
                    sel.repaint();
                    this.mainComponent.getDescendantOfName("radVerifyHostnameYes_25G").subscribe(details.ONVERIFYHOSTNAME, this, this.onselectedRadVerifyHostnameYes_25G);
                    this.mainComponent.getDescendantOfName("radVerifyHostnameNo_25G").subscribe(details.ONNOTVERIFYHOSTNAME, this, this.onselectedRadVerifyHostnameNo_25G);
                }
                this.jsxsuper(objEvent);
                this.callgetResourceConfigurationsInEnterprise();
            }

            details.prototype.ongetConfigDetailsSuccess = function(objEvent) {
                this.callgetResourceConfigurationsInEnterprise();
                var response = tibco.admin.Main.getServer().Cache.getDocument("xmlCacheIDGetSSLClientDetailsResponse_25G").getRecordNode("jsxroot");
                this.jsxsuper(response);

                var value = response.getAttribute("identity");
                this.mainComponent.getDescendantOfName("lblcIdentity_25G").setText(value, true);
                this.mainComponent.getDescendantOfName("selIdentity_25G").setValue(value, true);

                value = response.getAttribute("verifyHostname");
                this.mainComponent.getDescendantOfName("lblcVerifyHostname_25G").setText((value == "true") ? tibco.admin.Main.getServer().getDynamicProperty("@@radVerifyHostnameYes_25G") : tibco.admin.Main.getServer().getDynamicProperty("@@radVerifyHostnameNo_25G"), true);
                this.mainComponent.getDescendantOfName("radVerifyHostnameYes_25G").setGroupValue(value, true);
                if(value == "true") {
                    value = response.getAttribute("expectedHostname");
                    this.mainComponent.getDescendantOfName("lblcHostname_25G").setText(value, true);
                    this.mainComponent.getDescendantOfName("txtHostname_25G").setValue(value, true);
                }
                else {
                    this.mainComponent.getDescendantOfName("lblcHostname_25G").setText("", true);
                    this.mainComponent.getDescendantOfName("txtHostname_25G").setValue("", true);
                }

                value = response.getAttribute("ciperStrength");
                this.mainComponent.getDescendantOfName("selCipherStrength_25G").setValue(value, true);
                this.mainComponent.getDescendantOfName("lblcCipherStrength_25G").setText(this.mainComponent.getDescendantOfName("selCipherStrength_25G").getText(), true);

                value = response.getAttribute("certStoreURL");
                this.mainComponent.getDescendantOfName("lblcStoreURL_25G").setText(value, true);
                this.mainComponent.getDescendantOfName("txtStoreURL_25G").setValue(value, true);

                value = response.getAttribute("password");
                this.mainComponent.getDescendantOfName("txtPassword_25G").setValue(value, true);

                this.setEditable(true);
            }

            details.prototype.setEditRequestDetails = function(objService) {
                this.setRequestDetails(objService);
            }

            details.prototype.setAddRequestDetails = function(objService) {
                this.setRequestDetails(objService);
            }

            details.prototype.loadEdit = function() {
                this.jsxsuper();
                if(this.mainComponent.getDescendantOfName("radVerifyHostnameYes_25G").getGroupValue() == "false") {
                    this.mainComponent.getDescendantOfName("txtHostname_25G").setEnabled(jsx3.gui.Form.STATEDISABLED, true);
                }
            }

            details.prototype.setRequestDetails = function(objService) {
                objService.identity = this.mainComponent.getDescendantOfName("selIdentity_25G").getValue();
                objService.verifyHostname = this.mainComponent.getDescendantOfName("radVerifyHostnameYes_25G").getGroupValue();
                objService.expectedHostname = this.mainComponent.getDescendantOfName("txtHostname_25G").getValue();

                objService.storeType = "JKS";
                objService.ciperStrength = this.mainComponent.getDescendantOfName("selCipherStrength_25G").getValue();
                objService.certStoreURL = this.mainComponent.getDescendantOfName("txtStoreURL_25G").getValue();
                objService.password = this.mainComponent.getDescendantOfName("txtPassword_25G").getValue();
            }

            details.prototype.onselectedRadVerifyHostnameYes_25G = function() {
                this.mainComponent.getDescendantOfName("txtHostname_25G").setEnabled(jsx3.gui.Form.STATEENABLED, true);
            }
            details.prototype.onselectedRadVerifyHostnameNo_25G = function() {
                this.mainComponent.getDescendantOfName("txtHostname_25G").setEnabled(jsx3.gui.Form.STATEDISABLED, true);
            }

            //call this method to begin the service call (eg.service.callgetResourceConfigurationsInEnterprise();)
            details.prototype.callgetResourceConfigurationsInEnterprise = function() {
                var objService = tibco.admin.Main.loadService("tibco.admin",
                        "tibco/admin/enterpriseAssets/shdResDefs/rules/getPKI_IDENTITYResourceConfigurationsInEnterprise.xml",
                        "getPKIIdentityResourceConfigInEnterprise", "Get getPKI_IDENTITY Shared Resources Configurations")
                objService.setEndpointURL(tibco.admin.util.ServiceHelper.getSharedResourcesEndpointURL());

                objService.subscribe(jsx3.net.Service.ON_SUCCESS, this, this.ongetResourceConfigurationsInEnterpriseSuccess);
                objService.doCall();
            };

            details.prototype.ongetResourceConfigurationsInEnterpriseSuccess = function(objEvent) {
                this.mainComponent.getDescendantOfName("selIdentity_25G").repaint();
            };
        });
